package com.acme.service.bank;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import com.acme.domain.bank.Bank;
import com.acme.domain.bank.Client;
import com.acme.exceptions.ClientExistsException;

public class BankDataLoader {
	private BankService bankService;
	
	/**
	 * Loads a file which contains data feed in the following format: <br>
	 * accounttype=c|s;balance=100;overdraft=50;name=John;gender=m|f;
	 * 
	 * @param bank
	 * @param path
	 * @throws FileNotFoundException
	 */
	public void load(final Bank bank, final String path)
			throws FileNotFoundException {

		BankDataLoaderService loaderService = new BankDataLoaderService();
		ArrayList<Client> clients = null;
		Scanner in = new Scanner(new File(path));
		String s = "";
		
		while(in.hasNext()) {
			s+= in.nextLine() + "\n";
		}
		clients = loaderService.generateClients(s);
		in.close();
		
		if(clients == null) {
			return;
		}
		for(Client client: clients) {
			try {
				bankService.addClient(bank, client.getName(), client.getGender());
			} catch (ClientExistsException e) {
				e.printStackTrace();
			}
		}
	}
	
	public BankService getBankService() {
		return bankService;
	}

	public void setBankService(final BankService bankService) {
		this.bankService = bankService;
	}
}